/*
* 作者: wupengxin
* 创建时间:2016-08-16 14:30:10
* 说明: 写一个go版本的logstash 目前使用的logstash当读取大的日志文件的时候,会比较慢,在自己的测试过程中
* 当生成日志的速度在1M/S的时候,读取上送到es的速度慢了太多.
 */
package main

import (
	//"regexp"
	"outputplug"
	//"time"
	//"encoding/json"
	"inputplug"
	//"time"
	"common"
	"adaptplus"
)

func main() {
	// 读取输入插件
	var inplus inputplug.Input
	inconfs, err := common.GetInputConf("./conf.json")
	common.Exit(err)
	// 搜集所有输入插件返回的通道
	inchannels := make([]chan map[string]string, 0)

	for k, v := range inconfs{
		switch k {
		case "file":
			// 读取的配置的插件是file
			inplus = &inputplug.FileReader{}
			conf, _:= inplus.ConfigPlus(v)
			// 获取file的输出通道
			channels, e := inplus.GetInputStream(conf);
			common.Exit(e)
			for _, ch := range channels{
				inchannels = append(inchannels, ch)
			}
			inplus.Start()
		default:
			println("未实现该输入插件")
		}
	}

	println("len:", len(inchannels))
	// 根据输入通道配置输出通道
	var outplus outputplug.Output
	outconfs, err := common.GetOutConf("./conf.json")
	outchannels := make(map[string][]chan map[string]string)

	for k, v := range outconfs{
		switch k {
		case "elasticsearch":
			outchannels["elasticsearch"] = common.GenarateStringChannel(len(inchannels))
			outplus = &outputplug.EsWrite{}
			esconf, e:= outplus.ConfigPlus(v)
			common.Exit(e)
			outplus.PutOutStream(esconf, outchannels["elasticsearch"])
		case "stdout":
			outchannels["stdout"] = common.GenarateStringChannel(len(inchannels))
			outplus = &outputplug.StdOut{}
			stdconf, e := outplus.ConfigPlus(v)
			common.Exit(e)
			outplus.PutOutStream(stdconf, outchannels["stdout"])
		case "graylog":
			outchannels["graylog"] = common.GenarateStringChannel(len(inchannels))
			outplus = &outputplug.GrayLogClient{}
			grayconf, e := outplus.ConfigPlus(v)
			common.Exit(e)
			outplus.PutOutStream(grayconf, outchannels["graylog"])
		default:
			println("未实现此插件")
		}
	}

	// 开始进行输入插件和输出插件之间的适配
	adaptplus.ControlInOut(inchannels, outchannels)
	sleep := make(chan bool)
	<-sleep
}
